package com.hshit.downloader.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.hshit.downloader.searchengine.Mp3SkullEngine;
import com.hshit.downloader.searchengine.SearchEngine;

public class Utility {
	
	/**
	 * TODO for comment
	 */
	public static int getOrientationFromMediaStore(Context context, Uri imageUri) {
	    if(imageUri == null) {
	        return -1;
	    }
	    String[] projection = {MediaStore.Images.ImageColumns.ORIENTATION};
	    Cursor cursor = context.getContentResolver().query(imageUri, projection, null, null, null);
	    int orientation = -1;
	    if (cursor != null && cursor.moveToFirst()) {
	        orientation = cursor.getInt(0);
	        cursor.close();
	    }
	    return orientation;
	}
	/**
	 * TODO for comment
	 */
	public static String inputStreamToString(InputStream in) 
			throws IOException {
		String s = "";

		BufferedReader read = new BufferedReader(new InputStreamReader(in));

		String line = "";
		while ((line = read.readLine()) != null) {
			s += line;
		}
		return s;
	}
	
	public static boolean isEmpty(String in) {
		if (in == null || in.isEmpty()) {
			return true;
		}
		return false;
	}

	public static boolean isFuture(Date date) {
		if (date.compareTo(new Date()) > 0) {
			return true;
		}
		return false;
	}

	public static boolean isNetWorkAvailabel(Context context) {
		boolean status = false;
		try {
			ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo netInfo = cm.getNetworkInfo(0);
			if (netInfo != null
					&& netInfo.getState() == NetworkInfo.State.CONNECTED) {
				status = true;
			} else {
				netInfo = cm.getNetworkInfo(1);
				if (netInfo != null
						&& netInfo.getState() == NetworkInfo.State.CONNECTED)
					status = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return status;
	}
	
	public static SearchEngine getFromID(int engine) {
		SearchEngine mSearchEngine;
		switch (engine) {
		case Constants.ID_SE_SKULL:
			mSearchEngine = new Mp3SkullEngine();
			break;
		default:
			mSearchEngine = new Mp3SkullEngine();
			break;
		}
		return mSearchEngine;
	}
	
	public static void hideSoftInputFromWindow(View v, Activity mActivity) {
        InputMethodManager imm = (InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}
